package org.noote.libs.widget;

public class Widget_Carousel extends Widget_Window {

	final static int ANIM_COLUMN_DOWNRIGHT=0;
	final static int _DOCKING_NONE=0, _DOCKING_LEFT=1;
	
	Widget_Window _selectedItem;
	
	protected boolean _bInMove = false;
	protected int _iPressedPosition_X = 0, _iPressedPosition_Y = 0;
	protected boolean _bChangedSelection = false;
	protected int _iDocked = _DOCKING_NONE;
	
	WidgetEvent_OnChangeItem _eventOnChangeItem = null;
	
	int _iVirtualOffset_X=0, _iVirtualOffset_Y=0;
	int _iAnimation=ANIM_COLUMN_DOWNRIGHT;
	int _iColumns=0, _iLines=0;
	int _iVirtualColumns=0, _iVirtualLines=0;
	int _iColumnSize=0, _iLineSize=0;
	int _iItemSpacingX=5, _iItemSpacingY=5;
	
	public Widget_Carousel(Widget_Window parent) {
		super(parent);
	}
	
	public Widget_Carousel(Widget_Window parent, WidgetTheme theme) {
		super(parent, theme);
	}

	@Override
	public void create()
	{
		super.create();
		
		setBackground(_theme._textures.getTextureID(WidgetTheme.TEXTID_25P));
		setBackgroundColor(_theme.colorGray);
	}

	public void setRightDocked() {
		_iDocked = _DOCKING_LEFT;
		_iAnimation = ANIM_COLUMN_DOWNRIGHT;
		refresh();
	}
	
	public Widget_CarouselItem createCarouselItem()
	{
		Widget_CarouselItem item = new Widget_CarouselItem(this, _theme);
		item.create();
		return item;
	}
	
	public void setOnChangeItem(WidgetEvent_OnChangeItem event)
	{
		_eventOnChangeItem = event;
	}
	
	public boolean changedSelection()
	{
		return _bChangedSelection;
	}
	public Widget_Window getSelectedItem()
	{
		return _selectedItem;
	}
	public boolean selectItem(int iItem)
	{
		if(iItem>=0 && iItem<_children.size())
		{
			_bChangedSelection = true;
			_selectedItem = _children.get(iItem);
			_selectedItem.select(true);
			return true;
		}
		return true;
	}
	
	@Override
	protected void _checkButtons() {
		
		super._checkButtons();
		
		switch(_iAnimation)
		{
		case ANIM_COLUMN_DOWNRIGHT:
			_columnDownCheck();
			break;
		}
	}
	
	@Override
	protected void _checkEvents() {
		
		super._checkEvents();
		
		if(_eventOnChangeItem != null && haveNewEvent() && _iState==TOUCH_RELEASED && _selectedItem!=null)
		{
			if(_eventOnChangeItem.onSelectedItem(this, _selectedItem))
				resetNewEvent();
		}

	}
	
	protected void _columnDownCheck()
	{
		if(_iState == TOUCH_MOVED)
		{
			if(_iVirtualLines>_iLines)
			{
				_iVirtualOffset_X = 0;
				_iVirtualOffset_Y -= (_iPressedPosition_Y - _iTouch_Y);
				_bInMove = true;
			}
		}
		else if(_iState == TOUCH_PRESSED)
		{
			_iPressedPosition_X = _iTouch_X;
			_iPressedPosition_Y = _iTouch_Y;
			_iVirtualOffset_Y = 0;
		}
		else if(_iState == TOUCH_RELEASED)
		{
			if(_iAnimation == ANIM_COLUMN_DOWNRIGHT)
			{
				// no move
				if(!_bInMove || (_bInMove && _iVirtualOffset_Y<10))
				{
					_bNewEvent = true;
					
					if(_selectFromColumnPosition(_iVirtualOffset_X+_iTouch_X, _iVirtualOffset_Y+(_iMax_Y-_iTouch_Y)))
						_bNewEvent = true;
				}
				
				_bInMove = false;
			}
		}
	}
	
	protected boolean _selectFromColumnPosition(int iPositionX, int iPositionY) {
		for (Widget_Window window : _children)
		{
			window.select(false);
		}
		int c=iPositionX/_iColumnSize;
		int l=iPositionY/_iLineSize;
		int i = l*_iVirtualColumns+c;
		if(selectItem(i)) return true;
		_bChangedSelection = false;
		_selectedItem = null;
		return false;
	}

	@Override
	protected void _preRefresh()
	{
		super._preRefresh();
		
		switch(_iAnimation)
		{
		case ANIM_COLUMN_DOWNRIGHT:
			_columnDownSpacing();
			_columnDownRightPlacement();
			break;
		}
	}
	
	protected void _columnDownSpacing()
	{
		// find largest item size
		int max_sizex=0, max_sizey=0;
		for (Widget_Window pad : _children)
		{
			if(pad.getWidth()>max_sizex)
				max_sizex=pad.getWidth();
			if(pad.getHeight()>max_sizey)
				max_sizey=pad.getHeight();
		}
		
		// create column and line
		 _iColumnSize=max_sizex+_iItemSpacingX;
		 _iLineSize=max_sizey+_iItemSpacingY;
		 // need resizing ?
		 if(_iDocked==_DOCKING_LEFT)
		 {
			 setWorkSpace(_parent.getWidth()-(_iColumnSize+_iItemSpacingX), 0, _iColumnSize+_iItemSpacingX, _parent.getHeight());
		 }
		 _iColumns = getWidth()/_iColumnSize;
		 _iLines = getHeight()/_iLineSize;
		 if(_iColumns<1) _iColumns=1;
		 if(_iLines<1) _iLines=1;
		 
		 _iVirtualColumns=_children.size()/_iLines;
		 _iVirtualLines=_children.size()%_iLines;
		 if(_iVirtualColumns<1) _iVirtualColumns=1;
		 if(_iVirtualLines<1) _iVirtualLines=1;
	}
	
	protected void _columnDownRightPlacement()
	{
		int x=_iVirtualOffset_X;
		int y=_iMax_Y+_iVirtualOffset_Y;
		
		int iCount = 0;
		for (Widget_Window pad : _children)
		{
			pad.setPosition(x+_iItemSpacingX, y-(pad.getHeight()+_iItemSpacingY));
			
			if(iCount%_iVirtualColumns==0)
			{
				x = _iVirtualOffset_X;
				y -= _iLineSize;
			}
			else
				x += _iColumnSize;
			
			iCount++;
		}
	}
	
	@Override
	public void resetNewEvent() {
		super.resetNewEvent();
		_bChangedSelection = false;
	}
}
